package com.wj.algorithm.lintcode.erfen;

public class CopyOfP74 {

    public int findFirstBadVersion(int n) {
        if(n > 0) {

            int from = 1;
            int end = n;
            int mid = (from + end) / 2;
            boolean isFirstBad = SVNRepo.isBadVersion(1);
            
            if(!isFirstBad) {
                boolean isLastBad = SVNRepo.isBadVersion(n);
                if(isLastBad) {
                    while(mid > from && mid < end) {
                        boolean isMidBad = SVNRepo.isBadVersion(mid);
                        if(isMidBad) {
                            if(mid - from == 1) {
                                return mid;
                            }
                            end = mid;
                        } else {
                            if(end - mid == 1) {
                                return end;
                            }
                            from = mid;
                        }
                        mid = (from + end) / 2;
                    }
                } else {
                    return 0;
                }
                
            } else {
                return 1;
            }
        }
        return 0;
    }
    
    public static void main(String[] args) {
        System.out.println(new CopyOfP74().findFirstBadVersion(10));
    }
}
